import 'package:car_music/src/modules/home/my_home_controller.dart';
import 'package:flutter/material.dart';
import 'package:flutter_kit_dsp/flutter_kit_dsp.dart';
import 'package:flutter_kit_plus/flutter_kit.dart';

import '../../commont/button.dart';
import '../../commont/kcontainer.dart';
import '../../searchView.dart';
import '../../tool/assets.dart';
import 'db_setting.dart';

class MyHome extends HomePageDelegate<MyHomeController> {
  Widget _statusBtn() {
    return SizedBox(
      height: 30,
      child: Row(
        children: [
          Flexible(flex: 3, child: Container()),
          Container(
            padding: const EdgeInsets.only(bottom: 10),
            child: kMyTextButton(
              name: BluetoothManager.instance.connectedDeviceList.isNotEmpty ? 'ONLINE' : 'OFFLINE',
              textColor: BluetoothManager.instance.connectedDeviceList.isNotEmpty ? Colors.white : Color_red_btn,
              onClick: (value) {},
            ),
          ),
        ],
      ),
    );
  }

  List<BottomNavigationBarItem> get _navItems {
    Widget _icon(String img) {
      return Transform.translate(
          offset: const Offset(0, 10),
          child: Padding(
            padding: const EdgeInsets.only(top: 0, left: 5, right: 5, bottom: 0),
            child: Image.asset(img),
          ));
    }

    return List.generate(controller.items.length, (index) {
      return BottomNavigationBarItem(
        label: '', // 隐藏标签
        icon: _icon(controller.items[index]), // 默认状态的图标
        activeIcon: _icon(controller.items[index]), // 选中状态的图标
      );
    });
  }

  List<Routes> routes_list = [Routes.SUM, Routes.EQ, Routes.CROSSOVER, Routes.DELAY, Routes.COLOR];
  @override
  void onInit(BuildContext context) {
    super.onInit(context);
  }

  @override
  PreferredSizeWidget? get appBar {
    return PreferredSize(
      preferredSize: const Size.fromHeight(kToolbarHeight),
      child: Obx(() {
        final isHome = controller.isHome.value;
        final currentIndex = controller.currentIndex.value;

        return AppBar(
          title: isHome ? const SizedBox.shrink() : IgnorePointer(child: Text(controller.titles[currentIndex])),
          centerTitle: true,
          flexibleSpace: Builder(
            builder: (innerContext) => GestureDetector(
              onLongPress: () {
                LogRecorder.toggle(innerContext);
              }, // 使用 Builder 提供的 context
              child:
                  isHome ? Align(alignment: Alignment.bottomCenter, child: Image.asset(assets.logo, height: 40)) : null,
            ),
          ),
          leading: isHome
              ? const SizedBox.shrink()
              : BackButton(color: Colors.white, onPressed: () => controller.isHome.value = true),
          actions: [
            Builder(
              builder: (context) => IconButton(
                icon: const Icon(Icons.menu, color: Colors.white),
                tooltip: 'Open Right Drawer',
                onPressed: () {
                  FocusScope.of(context).unfocus();
                  Scaffold.of(context).openEndDrawer();
                },
              ),
            ),
          ],
        );
      }),
    );
  }

  @override
  Widget? get bottomNavigationBar {
    return Obx(() {
      return BottomNavigationBar(
        currentIndex: controller.currentIndex.value,
        onTap: (index) {
          controller.isHome.value = false;
          controller.currentIndex.value = index;
        },
        type: BottomNavigationBarType.fixed,
        showSelectedLabels: false,
        showUnselectedLabels: false,
        items: _navItems,
      );
    });
  }

  @override
  Widget? get endDrawer {
    return const LeftDrawer();
  }

  @override
  Widget? buildBody(BuildContext context, Widget child) {
    return SafeArea(child: child);
  }

  MyHome({super.key});

  Widget memoryView(BuildContext context) {
    return kContainer(
        child: Column(
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        Flexible(child: Container()),
        const Text('DEVICE MEMORY', style: TextStyle(fontSize: 18), textAlign: TextAlign.center),
        Flexible(child: Container()),
        Obx(() {
          List<String> memoryList = [...homeController.memory_tag_list];
          var selectMemoryTag = homeController.rxModel.value.tag;
          return Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: memoryList.asMap().entries.map(($0) {
              var tag = memoryList[$0.key];
              return Flexible(
                child: Container(
                  height: 30,
                  padding: const EdgeInsets.only(left: 5, right: 5),
                  child: kMyTextButton(
                    name: $0.value,
                    select: selectMemoryTag == tag,
                    onClick: (p0) => homeController.selectMemory(context, tag),
                  ),
                ),
              );
            }).toList(),
          );
        }),
        Flexible(child: Container()),
      ],
    ));
  }

  @override
  Widget build(BuildContext context) {
    Get.lazyPut(() => MyHomeController());
    return Obx(() {
      return controller.isHome.value
          ? Column(
              children: [
                _statusBtn(),
                Flexible(flex: 9, child: QBSettingView()),
                const SizedBox(height: 10),
                Expanded(
                    flex: 3,
                    child: kContainer(
                        child: controller.playView(assets.player, assets.pause, assets.last, assets.next,
                            const Size(50, 50), const Size(30, 30)))),
                const SizedBox(height: 10),
                Flexible(flex: 4, child: memoryView(context)),
              ],
            )
          : IndexedStack(
              index: controller.currentIndex.value,
              children: controller.pages,
            );
    });
  }
}
